﻿/* Copyright (c) 2010 Lexalytics Inc.

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel.Controls;
using Microsoft.Office.Tools.Excel.Extensions;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools.Ribbon;

// For internal dialogs
using Lexalytics4Excel.Dialogs;

namespace Lexalytics4Excel
{
    /// <summary>
    /// Class encapsulating Excel 2007 ribbon implementing LexaScope functionality
    /// </summary>
    public partial class LexalyticsRibbon : OfficeRibbon
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public LexalyticsRibbon()
        {
            InitializeComponent();

        }

        /// <summary>
        /// Event handler for ribbon load.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SalienceRibbon_Load(object sender, RibbonUIEventArgs e)
        {
        }

        /// <summary>
        /// Utility method to disable button (useful when there is no sheets open)
        /// </summary>
        public void DisableRibbon()
        {        
            ExcelContentButton.Enabled = false;
            //UpdateResultsButton.Enabled = false;
            
            GetThemesButton.Enabled = false;
            TopThemesButton.Enabled = false;
            FilteredThemesButton.Enabled = false;
            SortedThemesButton.Enabled = false;


            GetPeopleButton.Enabled = false;
            GetCompaniesButton.Enabled = false;
            GetProductsButton.Enabled = false;
            GetPlacesButton.Enabled = false;
        }

        /// <summary>
        /// Method to maintain state of ribbon buttons
        /// </summary>
        public void SetRibbonState()
        {
            bool bContentDataRange = false;
            bool bContentMultiCol = false;
            
            ExcelContentButton.Enabled = true;


            if (Globals.LexalyticsAddIn.CurrentProject != null)
            {
                bContentDataRange = Globals.LexalyticsAddIn.CurrentProject.HasData;
                bContentMultiCol = Globals.LexalyticsAddIn.CurrentProject.MultiColumn;
            }

            //UpdateResultsButton.Enabled = bContentDataRange;
            GetThemesButton.Enabled = bContentDataRange;
            TopThemesButton.Enabled = bContentDataRange;
            FilteredThemesButton.Enabled = bContentDataRange && bContentMultiCol;
            SortedThemesButton.Enabled = bContentDataRange && bContentMultiCol;

            GetPeopleButton.Enabled = bContentDataRange;
            GetTopPeopleButton.Enabled = bContentDataRange;
            GetFilteredPeopleButton.Enabled = bContentDataRange && bContentMultiCol;
            GetSortedPeopleButton.Enabled = bContentDataRange && bContentMultiCol;

            GetCompaniesButton.Enabled = bContentDataRange;
            GetTopCompaniesButton.Enabled = bContentDataRange;
            GetFilteredCompaniesButton.Enabled = bContentDataRange && bContentMultiCol;
            GetSortedCompaniesButton.Enabled = bContentDataRange && bContentMultiCol;

            GetPlacesButton.Enabled = bContentDataRange;
            GetTopPlacesButton.Enabled = bContentDataRange;
            GetFilteredPlacesButton.Enabled = bContentDataRange && bContentMultiCol;
            GetSortedPlacesButton.Enabled = bContentDataRange && bContentMultiCol;

            GetProductsButton.Enabled = bContentDataRange;
            GetTopProductsButton.Enabled = bContentDataRange;
            GetFilteredProductsButton.Enabled = bContentDataRange && bContentMultiCol;
            GetSortedProductsButton.Enabled = bContentDataRange && bContentMultiCol;
        }

        #region Settings button handlers
        /// <summary>
        /// Handler for Select Data button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExcelContentButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
//                // If we don't have a project name, create one and open project
//                if (Globals.LexalyticsAddIn.CurrentProject.ProjectName.Length == 0)
//                    Globals.LexalyticsAddIn.CurrentProject.CreateProject();

                // Get selected range and send to dataproject object for work
                Excel.Range r = Globals.LexalyticsAddIn.Application.ActiveWindow.RangeSelection;
                Globals.LexalyticsAddIn.CurrentProject.SelectData(r);
                SetRibbonState();
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem selecting the data for the project.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void UpdateResultsButton_Click(object sender, RibbonControlEventArgs e)
        {
            // Pass to DataProject member for work
            Globals.LexalyticsAddIn.CurrentProject.DoUpdate();
        }

        #endregion // Settings buttons handlers

        #region Text analytics button handlers

        /// <summary>
        /// Handler for Themes split button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetThemesSplitButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonSplitButton theChoice = (RibbonSplitButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetThemes(theChoiceType);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting themes.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handler for main Themes button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetThemesButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonButton theChoice = (RibbonButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetThemes(theChoiceType);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting top themes.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handler for People split button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetPeopleSplitButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonSplitButton theChoice = (RibbonSplitButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetEntities(theChoiceType, (int)LexaScopeNET.LXAEntityType.Types.Person);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting people.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

        /// <summary>
        /// Handler for main People button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetPeopleButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonButton theChoice = (RibbonButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetEntities(theChoiceType, (int)LexaScopeNET.LXAEntityType.Types.Person);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting top people.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handler for Companies split button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetCompaniesSplitButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonSplitButton theChoice = (RibbonSplitButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetEntities(theChoiceType, (int)LexaScopeNET.LXAEntityType.Types.Company);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting companies.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handler for main Companies button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetCompaniesButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonButton theChoice = (RibbonButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetEntities(theChoiceType, (int)LexaScopeNET.LXAEntityType.Types.Company);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting top companies.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handler for main Places button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetPlacesSplitButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonSplitButton theChoice = (RibbonSplitButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetEntities(theChoiceType, (int)LexaScopeNET.LXAEntityType.Types.Place);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting places.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handler for main Places button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetPlacesButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonButton theChoice = (RibbonButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetEntities(theChoiceType, (int)LexaScopeNET.LXAEntityType.Types.Place);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting top places.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handler for main Products button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetProductsSplitButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonSplitButton theChoice = (RibbonSplitButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetEntities(theChoiceType, (int)LexaScopeNET.LXAEntityType.Types.Product);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting products.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

        /// <summary>
        /// Handler for main Products button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetProductsButton_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                RibbonButton theChoice = (RibbonButton)sender;
                int theChoiceType = Convert.ToInt16(theChoice.Tag);

                // Pass to DataProject member for work
                Globals.LexalyticsAddIn.CurrentProject.GetEntities(theChoiceType, (int)LexaScopeNET.LXAEntityType.Types.Product);
            }
            catch (Exception err)
            {
                MessageBox.Show("There was a problem with getting top products.\n" + err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handler for Options dialog launcher
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OptionsButton_Click(object sender, RibbonControlEventArgs e)
        {
            // Pass to DataProject member for work
            Globals.LexalyticsAddIn.CurrentProject.DoOptions(false);
        }

        /// <summary>
        /// Handler for About Box launcher
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AboutButton_Click(object sender, RibbonControlEventArgs e)
        {
            // Pass to DataProject member for work
            Globals.LexalyticsAddIn.CurrentProject.DoAbout();
        }

        #endregion // Text analytics button handlers

    }
}
